/*
	Robert Elliott
	relliott249@gmail.com


	ECEN 4013 
	Senior Design TEAM 2: Bearshark

	This is the sudo code that will be used for Team 2's mage IR code

	MAGE uses a 56kHz modulated signal for data transfer.
	Each data bit is 3.33ms long in its duration. 
	Math:
		1/56,000 = 18us period -> 9us on 9us off 
		3.33ms/18us = 185 cycles 

	Sources used for this code:
		http://mage.okstate.edu/doc/ir/MIRP.pdf
		
	Musical Inspiration:
	IR low & high	Common Reactor - Silversun Pickups
	sendByte	Juntura - Circa Survive
*/

void irHigh(void)
{
	int i;	// counter variable

	for(i = 0; i <  185; i ++)
	{
		ir = HIGH;		// turn on IR led
		delayus(9);		// delay for 9us
		ir = LOW;		// turn off IR led
		delayus(9);		// delay for 9us
	}					// repeat for duration of databit
}	// end of irHigh


void irLow(void)
{
	ir = LOW;			// IR led goes low
	delayus(3333);		// stay off for 3.33ms
}

/*
	 This function receives an integer corresponding to a byte of data
	 that is to be transmitted over IR

	 This function then breaks apart the byte by doing bitwise comparisons
	 sending IR high and low packets corresponding to the 1's and 0's
	 that make up the byte of the received integer
*/
void sendByte(int data)
{
	// to comply with MAGE IR standards we first send a high bit
	irHigh();
	// then we send a low bit
	irLow();
	// now we can send out the single byte packet

	if(data && 0x80 == 0x80)	// if the first bit is a 1
		irHigh();				// then send a high pulse
	else
		irLow();				// else send a low signal
	if(data && 0x40 == 0x40)	// if the second bit is a 1
		irHigh();				// send high pulse
	else
		irLow();				// esle low
	if(data && 0x20 == 0x20)	// if third bit is a 1
		irHigh();				// send high
	else
		irLow();				// else low
	if(data && 0x10 == 0x10)	// if fourth bit is 1
		irHigh();				// send high pulse
	else
		irLow();				// send low pulse
	if(data && 0x08 == 0x08)	// if 5th bit is a 1
		irHigh();				// send high pulse
	else
		irLow();				// else send low pulse
	if(data && 0x06 == 0x06)	// if 6th bit == 1
		irHigh();
	else
		irLow();
	if(data && 0x04 == 0x04)	// 6th bit == 1
		irHigh();
	else 
		irLow();
	if(data && 0x02 == 0x02)	// 7th bit == 1
		irHigh();
	else 
		irLow();
	if(data && 0x01 == 0x01)	// 8th bit == 1
		irHigh();
	else
		irLow();

	// high bit for even parity as stated in MIRP documentation
	irHigh();

}	// end of sendByte
